/*
 * Copyright (C) 2017 Spreadtrum Communications Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "sprd_panel.h"
#include "sprd_dsi.h"
#include "dsi/mipi_dsi_api.h"
#include "sprd_dphy.h"
#include <i2c.h>

static uint8_t init_data[] = {
	0x39, 0x00, 0x00, 0x02, 0xb0, 0x00,
	0x39, 0x00, 0x00, 0x04, 0xB3, 0x31, 0x00, 0x06,
 	0x39, 0x00, 0x00, 0x02, 0xb4, 0x00,
	0x39, 0x00, 0x00, 0x06, 0xB6, 0x33, 0x5B, 0x81, 0x72, 0x00,
	0x39, 0x00, 0x00, 0x08, 0xB8, 0x57, 0x3D, 0x19, 0x1E, 0x0A, 0x50, 0x50,
	0x39, 0x00, 0x00, 0x08, 0xB9, 0x6F, 0x3D, 0x28, 0x3C, 0x14, 0xC8, 0xC8,
	0x39, 0x00, 0x00, 0x08, 0xBA, 0xB5, 0x33, 0x41, 0x64, 0x23, 0xA0, 0xA0,
	0x39, 0x00, 0x00, 0x03, 0xBB, 0x14, 0x14,
	0x39, 0x00, 0x00, 0x03, 0xBC, 0x37, 0x32,
	0x39, 0x00, 0x00, 0x03, 0xBD, 0x64, 0x32,
	0x39, 0x00, 0x00, 0x02, 0xBE, 0x04,
	0x39, 0x00, 0x00, 0x02, 0xC0, 0x00,
	0x39, 0x00, 0x00, 0x31, 0xC1, 0x04, 0x40, 0x00, 0x00, 0x26, 0x15, 0x19, 0x0B, 0x63, 0xD2, 0xD9, 0x9A, 0x73, 0xEF, 0xBD, 0xE7, 0x5C, 0x6B, 0x93, 0x4D, 0x22, 0x18, 
	      0x8B, 0x2A, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02, 0x22, 0x1B, 0x06, 0x03, 0x00, 0x0F, 0xFF, 0x00, 0x01, 0x00, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x19,	0xC2, 0x01, 0xF8, 0x70, 0x08, 0x68, 0x08, 0x0C, 0x10, 0x00, 0x08, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 
	      0x00, 0x00,
	0x39, 0x00, 0x00, 0x3f, 0xC3, 0x86, 0xD8, 0x6D, 0x86, 0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x24, 0x42, 0x00, 0x00, 0x48, 0x84, 0x88, 0x00, 0x00, 0x01, 0x01, 
	      0x03, 0x28, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x19, 0x00, 0x19, 0x00, 0x00, 0x00, 0x19, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	      0x32, 0x00, 0x32, 0x00, 0x5A, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
	0x39, 0x00, 0x00, 0x15, 0xC4, 0x70, 0x00, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x00, 0x02, 0x02, 0x31, 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x01, 0x01,
	0x39, 0x00, 0x00, 0x09, 0xC5, 0x08, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x40, 0xC6, 0x42, 0x21, 0x21, 0x05, 0x3D, 0x05, 0x3D, 0x01, 0x02, 0x01, 0x02, 0x05, 0x05, 0x00, 0x00, 0x05, 0x05, 0x0B, 0x0C, 0x05, 0x42, 0x00, 
	0x6E, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x27, 0xC7, 0x00, 0x1F, 0x30, 0x43, 0x52, 0x5C, 0x72, 0x80, 0x8B, 0x95, 0x45, 0x4F, 0x5C, 0x70, 0x79, 0x85, 0x90, 0x93, 0xBF, 0x00, 0x1F, 0x30, 
	      0x43, 0x52, 0x5C, 0x72, 0x80, 0x8B, 0x95, 0x45, 0x4F, 0x5C, 0x70, 0x79, 0x85, 0x90, 0x93, 0xBF,
	0x39, 0x00, 0x00, 0x38, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 
	      0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 
	      0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00,
	0x39, 0x00, 0x00, 0x14, 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00,
	0x39, 0x00, 0x00, 0x2c, 0xCA, 0x1C, 0xFC, 0xFC, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x1c, 0xCB, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x08, 0x00, 0x01, 0x00, 0x31, 0xF0, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	      0x00, 0x00, 0x00, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x03, 0xCC, 0x02, 0x01,
	0x39, 0x00, 0x00, 0x27, 0xCD, 0x25, 0x00, 0x28, 0x00, 0x28, 0x00, 0x5C, 0x02, 0xDF, 0xDF, 0xE0, 0xE0, 0xD6, 0xD6, 0xE0, 0xE0, 0x01, 0x00, 0x00, 0x00, 0x32, 0x00, 
	      0x32, 0x00, 0x5D, 0x02, 0x24, 0x24, 0x01, 0x33, 0x00, 0x33, 0x00, 0x5E, 0x02, 0x24, 0x24, 0xA2,
	0x39, 0x00, 0x00, 0x1a, 0xCE, 0x5D, 0x40, 0x49, 0x53, 0x59, 0x5E, 0x63, 0x68, 0x6E, 0x74, 0x7E, 0x8A, 0x98, 0xA8, 0xBB, 0xD0, 0xFF, 0x04, 0x00, 0x04, 0x04, 0x42, 
	      0x00, 0x69, 0x5A,
	0x39, 0x00, 0x00, 0x03, 0xCF, 0x4A, 0x1D,
	0x39, 0x00, 0x00, 0x14, 0xD0, 0x33, 0x57, 0xD4, 0x31, 0x01, 0x10, 0x10, 0x10, 0x19, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x65,
	0x39, 0x00, 0x00, 0x02, 0xd1, 0x00,
	0x39, 0x00, 0x00, 0x16, 0xD3, 0x1B, 0x3B, 0xBB, 0x77, 0x77, 0x77, 0xBB, 0xB3, 0x33, 0x00, 0x00, 0x6D, 0x6E, 0xC7, 0xC7, 0x33, 0xBB, 0xF2, 0xFD, 0xC6, 0x0B,
	0x39, 0x00, 0x00, 0x0b, 0xD5, 0x03, 0x00, 0x00, 0x02, 0x0E, 0x02, 0x0E, 0x01, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x02, 0xd6, 0x01,
	0x39, 0x00, 0x00, 0x22, 0xD7, 0xF6, 0xFF, 0x03, 0x05, 0x41, 0x24, 0x80, 0x1F, 0xC7, 0x1F, 0x1B, 0x00, 0x0C, 0x07, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 
	0x1F, 0x00, 0xFC, 0x00, 0x00, 0xAB, 0x67, 0x7E, 0x5D, 0x26, 0x00,
	0x39, 0x00, 0x00, 0x08, 0xD9, 0x00, 0x16, 0x14, 0x3F, 0x0F, 0x57, 0x04,
	0x39, 0x00, 0x00, 0x05, 0xDD, 0x30, 0x06, 0x23, 0x65,
        0x39, 0x00, 0x00, 0x05, 0xDE, 0x00, 0x3F, 0xFF, 0x90,
	0x39, 0x00, 0x00, 0x02, 0xea, 0x1f,
	0x39, 0x00, 0x00, 0x04, 0xEE, 0x41, 0x51, 0x00,
	0x39, 0x00, 0x00, 0x04, 0xF1, 0x00, 0x00, 0x00,
	0x15, 0x00, 0x00, 0x02, 0x35, 0x00,
	0x05, 0x14, 0x00, 0x01, 0x29,
	0x05, 0x64, 0x00, 0x01, 0x11,
	CMD_END
};

static int mipi_dsi_send_cmds(struct sprd_dsi *dsi, void *data)
{
	uint16_t len;
	struct dsi_cmd_desc *cmds = data;

	if ((cmds == NULL) || (dsi == NULL))
		return -1;

	for (; cmds->data_type != CMD_END;) {
		len = (cmds->wc_h << 8) | cmds->wc_l;
		mipi_dsi_dcs_write(dsi, cmds->payload, len);
		if (cmds->wait)
			msleep(cmds->wait);
		cmds = (struct dsi_cmd_desc *)(cmds->payload + len);
	}
	return 0;
}

static int td4310_init(void)
{
	struct sprd_dsi *dsi = &dsi_device;
	struct sprd_dphy *dphy = &dphy_device;

	mipi_dsi_lp_cmd_enable(dsi, true);
	mipi_dsi_send_cmds(dsi, init_data);
	mipi_dsi_set_work_mode(dsi, SPRD_MIPI_MODE_VIDEO);
	mipi_dsi_state_reset(dsi);
	mipi_dphy_hs_clk_en(dphy, true);

	return 0;
}

static int td4310_readid(void)
{
	struct sprd_dsi *dsi = &dsi_device;
	uint8_t read_buf[5] = {0};

	mipi_dsi_lp_cmd_enable(dsi, true);
	mipi_dsi_set_max_return_size(dsi, 5);
	mipi_dsi_dcs_read(dsi, 0xbf, read_buf, 5);

	if((0x02 == read_buf[0]) && (0x3C == read_buf[1]) &&
	   (0xc0 == read_buf[2]) && (0x09 == read_buf[3])) {
		pr_info("td4310 read id success!\n");
		return 0;
        }
	pr_err("td4310 read id failed:0x%x,0x%x,0x%x,0x%x\n",read_buf[0],read_buf[1],read_buf[2],read_buf[3]);
	return -1;
}

static int td4310_power(int on)
{
	if (on) {
#ifdef CONFIG_LCM_GPIO_AVDDEN
                sprd_gpio_request(NULL, CONFIG_LCM_GPIO_AVDDEN);
                sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_AVDDEN, 1);
                mdelay(10);
#endif
#ifdef CONFIG_LCM_GPIO_AVEEEN
                sprd_gpio_request(NULL, CONFIG_LCM_GPIO_AVEEEN);
                sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_AVEEEN, 1);
                mdelay(20);
#endif
		sprd_gpio_request(NULL, CONFIG_LCM_GPIO_RSTN);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 1);
		mdelay(5);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 0);
		mdelay(5);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 1);
		mdelay(20);
	} else {
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 0);
		mdelay(5);
	}

	return 0;
}

static struct panel_ops td4310_ops = {
	.init = td4310_init,
	.read_id = td4310_readid,
	.power = td4310_power,
};

static struct panel_info td4310_info = {
	/* common parameters */
	.lcd_name = "lcd_td4310_truly_mipi_fhd",
	.type = SPRD_PANEL_TYPE_MIPI,
	.bpp = 24,
//	.fps = 60,
	.width = 1080,
	.height = 2160,

	/* DPI specific parameters */
	.pixel_clk = 153600000, /*Hz*/
	.rgb_timing = {
		.hfp = 65,
		.hbp = 20,
		.hsync = 2,
		.vfp = 2,
		.vbp = 28,
		.vsync = 4,
	},

	/* MIPI DSI specific parameters */
#ifdef CONFIG_LCD_MIPI_CLOCK
	.phy_freq = CONFIG_LCD_MIPI_CLOCK,
#else
	.phy_freq = 1100000,
#endif
	.lane_num = 4,
	.work_mode = SPRD_MIPI_MODE_VIDEO,
	.burst_mode = VIDEO_BURST_WITH_SYNC_PULSES,
	.nc_clk_en = false,
};

struct panel_driver td4310_truly_driver = {
	.info = &td4310_info,
	.ops = &td4310_ops,
};
